package com.gt.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.app.domain.EsColonScreenProject;
import com.gt.app.domain.EsColonScreenProjectUser;
import com.gt.app.domain.dto.ColonProjectDto;
import com.gt.app.domain.vo.ColonScreenProjectVo;
import com.gt.app.mapper.EsColonScreenProjectMapper;
import com.gt.app.service.EsColonScreenProjectService;
import com.gt.app.service.EsColonScreenProjectUserService;
import com.gt.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 结肠癌筛查项目 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsColonScreenProjectServiceImpl extends ServiceImpl<EsColonScreenProjectMapper, EsColonScreenProject> implements EsColonScreenProjectService {

    @Autowired
    private EsColonScreenProjectUserService esColonScreenProjectUserService;

    @Override
    public List<ColonScreenProjectVo> getColonScreenProject(String checkId) {

        ArrayList<ColonScreenProjectVo> esColonScreenProjects = new ArrayList<>();
        List<EsColonScreenProject> projects = this.list();
        LambdaQueryWrapper<EsColonScreenProjectUser> eq = new LambdaQueryWrapper<EsColonScreenProjectUser>().eq(EsColonScreenProjectUser::getCheckId, checkId);
        List<EsColonScreenProjectUser> projectUsers = esColonScreenProjectUserService.list(eq);
        for (EsColonScreenProject project : projects) {
            ColonScreenProjectVo colonScreenProjectVo = new ColonScreenProjectVo();
            colonScreenProjectVo.setProjectId(project.getId());
            colonScreenProjectVo.setProject(project.getProject());
            colonScreenProjectVo.setIcon(project.getIcon());
            colonScreenProjectVo.setPictureUrl(project.getPictureUrl());
            colonScreenProjectVo.setButtonText(project.getButtonText());
            colonScreenProjectVo.setLabelText(project.getLabelText());
            colonScreenProjectVo.setUrl(project.getUrl());
            colonScreenProjectVo.setStatus("0");
            if (projectUsers != null){
                for (EsColonScreenProjectUser projectUser : projectUsers) {
                    if (project.getId() == projectUser.getProjectId()){
                        BeanUtils.copyProperties(projectUser,colonScreenProjectVo);
                        break;
                    }
                }
            }
            esColonScreenProjects.add(colonScreenProjectVo);
        }
        return esColonScreenProjects;
    }

    @Override
    public void addColonScreenProjectUser(ColonProjectDto colonProjectDto) {
        LambdaQueryWrapper<EsColonScreenProjectUser> eq = new LambdaQueryWrapper<EsColonScreenProjectUser>()
                .eq(EsColonScreenProjectUser::getCheckId, colonProjectDto.getCheckId())
                .eq(EsColonScreenProjectUser::getProjectId,colonProjectDto.getProjectId());
        EsColonScreenProjectUser projectUser = esColonScreenProjectUserService.getOne(eq);

        if (projectUser == null){
            LambdaQueryWrapper<EsColonScreenProject> one = new LambdaQueryWrapper<EsColonScreenProject>().eq(EsColonScreenProject::getId, colonProjectDto.getProjectId());
            EsColonScreenProject project = this.getOne(one);
            if (project != null){
                EsColonScreenProjectUser esColonScreenProjectUser = new EsColonScreenProjectUser();
                esColonScreenProjectUser.setCheckId(colonProjectDto.getCheckId());
                esColonScreenProjectUser.setProjectId(colonProjectDto.getProjectId());
                esColonScreenProjectUser.setUserId(SecurityUtils.getUserId());
                /**1-代表填写中**/
                esColonScreenProjectUser.setStatus("1");
               /* if ("结肠癌筛查调查问卷".equals(project.getProject())) {
                    esColonScreenProjectUser.setEvaluateResult("高风险");
                }
                if ("结肠癌筛查预约".equals(project.getProject())) {
                    esColonScreenProjectUser.setEvaluateResult("2023/09/25(遵义医科大学附属医院内镜中心)");
                }
                if ("结肠癌筛查检查结果".equals(project.getProject())) {
                    esColonScreenProjectUser.setEvaluateResult("2023/09/25(遵义医科大学附属医院内镜中心)");
                }
                if ("结肠癌筛查检查随访".equals(project.getProject())) {
                    esColonScreenProjectUser.setFollowDate("2023年10月25日(未填写)");
                }*/
                esColonScreenProjectUserService.save(esColonScreenProjectUser);
            }
        }
    }
}
